package com.education.demouse.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.education.common.pojo.AjaxResult;
import com.education.demouse.excel.ExcelListener;
import com.education.demouse.excel.FreezeAndFilter;
import com.education.demouse.pojo.model.StudentAnswerDetails;
import com.education.demouse.service.IStudentAnswerDetailsService;
import com.education.welco.pojo.OrderPaper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 老师查看学生答题详情 controller业务
 *
 * @Author: liftsail
 * @Date: 2022-10-20
 * @Description: 不积跬步无以至千里
 */
@RestController
@RequestMapping("/studentAnswerDetails")
public class StudentAnswerDetailsController {

    @Autowired
    private IStudentAnswerDetailsService studentAnswerDetailsService;

    @Autowired
    private ExcelListener<StudentAnswerDetails> excelListener;

    /**
     * 列表查询老师查看学生答题详情
     */
    @GetMapping("/list")
    public AjaxResult list(StudentAnswerDetails studentAnswerDetails, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<StudentAnswerDetails> list = studentAnswerDetailsService.selectStudentAnswerDetailsList(studentAnswerDetails);
        PageInfo pageInfo = new PageInfo<>(list);
        return AjaxResult.success("查询成功！", pageInfo);
    }


    /**
     * 新增保存老师查看学生答题详情
     */
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody StudentAnswerDetails studentAnswerDetails) {
        studentAnswerDetailsService.insertStudentAnswerDetails(studentAnswerDetails);
        return AjaxResult.success("添加成功！");
    }


    /**
     * 主键详情老师查看学生答题详情
     */
    @GetMapping("/selectOne")
    public AjaxResult selectOne(Integer id) {
        StudentAnswerDetails result = studentAnswerDetailsService.selectStudentAnswerDetailsById(id);
        return AjaxResult.success("查询成功！", result);
    }

    /**
     * 修改保存老师查看学生答题详情
     */
    @PostMapping("/update")
    public AjaxResult editSave(@RequestBody StudentAnswerDetails studentAnswerDetails) {
        studentAnswerDetailsService.updateStudentAnswerDetails(studentAnswerDetails);
        return AjaxResult.success("修改成功！");
    }

    /**
     * 删除老师查看学生答题详情
     */
    @PostMapping("/del")
    public AjaxResult remove(String ids) {
        studentAnswerDetailsService.deleteStudentAnswerDetailsByIds(ids);
        return AjaxResult.success("删除成功！");
    }

    /**
     * 根据老师id查找试卷列表
     *
     * @param teaId
     * @return
     */
    @GetMapping("/queryPaperListByTeaId")
    public AjaxResult queryPaperListByTeaId(Integer teaId) {
        if (teaId != null) {
            List<OrderPaper> paperList = studentAnswerDetailsService.queryPaperListByTeaId(teaId);
            return AjaxResult.success(paperList);
        }
        return AjaxResult.error("缺少teaId参数");
    }

    /**
     * 根据老师id查询学生答题详情列表
     * 班级、试卷筛选
     *
     * @param teaId
     * @param classId
     * @param paperId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/queryScoreListByTeaUserId")
    public AjaxResult queryListByTeaUserId(Integer teaId, Integer classId, Integer paperId, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<StudentAnswerDetails> studentAnswerDetailsList = studentAnswerDetailsService.queryListByTeaUserId(teaId, classId, paperId);
        PageInfo<StudentAnswerDetails> pageInfo = new PageInfo<>(studentAnswerDetailsList);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 教师端-成绩分析-成绩管理分析
     * 根据老师id查询学生答题统计列表
     *
     * @param teaId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/queryStatisticsListByTeaUserId")
    public AjaxResult queryStatisticsListByTeaUserId(Integer teaId, Integer classId, Integer paperId, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<StudentAnswerDetails> studentAnswerDetailsList = studentAnswerDetailsService.queryStatisticsListByTeaUserId(teaId, classId, paperId);
        PageInfo<StudentAnswerDetails> pageInfo = new PageInfo<>(studentAnswerDetailsList);
        return AjaxResult.success(pageInfo);
    }

    //导出(勾选导出-id)
    @PostMapping("/downloadExcel")
    public void doDownLoad(HttpServletResponse response, Integer teaId, String ids) throws IOException {
        List<StudentAnswerDetails> studentAnswerDetailsList = studentAnswerDetailsService.queryListByIds(teaId, ids);

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("学生成绩统计表单", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), StudentAnswerDetails.class).sheet("用户数据")
                .registerWriteHandler(new FreezeAndFilter()).registerWriteHandler(horizontalCellStyleStrategy())
                .doWrite(studentAnswerDetailsList);
        //EasyExcel.write(response.getOutputStream(), UserEntity.class).sheet("模板").doWrite(getData());
    }

    //导入
    @PostMapping("/uploadExcel")
    public AjaxResult upLoad(MultipartFile file) throws IOException {
        //EasyExcel.read(file.getInputStream(), UserEntity.class, new UserListener()).sheet().doRead();
        //EasyExcel.read(file.getInputStream(), UserEntity.class, userListener).sheet().doRead();
        EasyExcel.read(file.getInputStream(), StudentAnswerDetails.class, excelListener).sheet().doRead();
        return AjaxResult.success("导入成功");
    }

    //导出模板
    @PostMapping("/downloadExcelDemo")
    public void downloadExcelDemo(HttpServletResponse response) throws IOException {
        //response.setContentType("application/vnd.ms-excel");
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("学生成绩统计表单模板", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), StudentAnswerDetails.class).sheet("用户数据")
                .registerWriteHandler(new FreezeAndFilter()).registerWriteHandler(horizontalCellStyleStrategy())
                .doWrite(new ArrayList<StudentAnswerDetails>());
    }

    public HorizontalCellStyleStrategy horizontalCellStyleStrategy() {
        //1、头
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        //设置头字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 13);
        headWriteFont.setBold(true);
        headWriteCellStyle.setWriteFont(headWriteFont);
        //设置头居中
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        //2、内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置 水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //设置边框样式
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);//细实线
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);

        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }


}